package dfbz.com.service.impl;

import dfbz.com.dao.UserDao;
import dfbz.com.dao.UserInfoDao;
import dfbz.com.model.User;
import dfbz.com.model.UserInfo;
import dfbz.com.service.UserService;

import java.util.Date;
import java.util.Map;

public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDao();

    private UserInfoDao userInfoDao = new UserInfoDao();


    @Override
    public User findUserByName(String username) {
        return userDao.findUserByName(username);
    }

    @Override
    public Map<String, Object> findUserBaseById(Integer userId) {
        return userDao.findUserById(userId);
    }

    @Override
    public User findUserByEmail(String email) {

        return userDao.findUserByEmail(email);
    }

    @Override
    public void addUser(String email, String username, String password) {
        User user = new User();
        user.setId(null);
        user.setEmail(email);
        user.setUsername(username);
        user.setPassword(password);
        user.setIsSecret("0");
        userDao.add(user);
        User dbUser = userDao.findUserByName(username);

        //给当前用户初始化用户信息 + 登录信息 创建时间

        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(dbUser.getId());
        Date date = new Date();
        userInfo.setRegisterTime(date);
        userInfo.setLoginTime(date);
        userInfoDao.add(userInfo);
    }

    @Override
    public void resetPassword(Integer id, String password) {
        User user = new User();
        user.setPassword(password);
        userDao.update(user);


    }
}
